package springboot.study.project.util.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: DateUtil
 * @Description: 日期工具类
 * @Author: baoxia
 * @Date: 2020-07-09 22:07
 * @Version: 1.0
 **/
public class DateUtil {

    final static int DayDIFF = 86400000;
    final static int WEEKDIFF = 604800000;

    public final static String PATTERN_N = "yyyy-MM";
    public final static String PATTERN_NY = "yyyy-MM";
    public final static String PATTERN_NYR = "yyyy-MM-dd";
    public final static String PATTERN_NYRSFM = "yyyy-MM-dd HH:mm:ss";


    /**
     * 获取指定格式的时间字符串
     *
     * @param pattern
     * @param date
     * @return
     */
    public static String getDateStr(String pattern, Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    /**
     * 日期字符串转时间戳
     *
     * @param pattern
     * @param dateStr
     * @return
     */
    public static Long dateStrToTimestamp(String pattern, String dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Long timestamp = date.getTime();
        return timestamp;
    }


    /**
     * 将时间戳转换为Unix日期格式
     *
     * @param timestamp 时间戳
     * @return 转换后的Unix时间字符串，eg：2020-07-09T21:24:28.000Z
     */
    public static String getUnixDate(Long timestamp) {
        String str;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String str_1 = dateFormat.format(new Date(timestamp));
        String str_2 = timeFormat.format(new Date(timestamp));
        str = str_1 + "T" + str_2 + ".00Z";
        System.out.println(str);
        //此处增加"000"是因为Date类中，传入的时间单位为毫秒。
        return str;
    }

    /**
     * 计算上月第一天 0点的时间戳
     *
     * @return
     * @throws ParseException
     */
    public static Long getLastMonthBegin() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) - 1, 1, 0, 0, 0);
        long tt = calendar.getTime().getTime();
        return tt;
    }

    /**
     * 计算上月最后一天 23点的时间戳
     *
     * @return
     * @throws ParseException
     */
    public static Long getLastMonthEnd() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) - 1, calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        long tt = calendar.getTime().getTime();
        return tt;
    }


    /**
     * 计算上周周一 0点的时间戳
     *
     * @return
     * @throws ParseException
     */
    public static Long getLastWeekBegin() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) - 1, 0, 0, 0);
        calendar.get(Calendar.DAY_OF_WEEK);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.add(Calendar.DATE, -7);
        return calendar.getTime().getTime();
    }

    /**
     * 计算上周周日23点的时间戳
     *
     * @return
     * @throws ParseException
     */
    public static Long getLastWeekEnd() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime().getTime();
    }


    /**
     * 获取指定日期所在月份开始的时间戳
     *
     * @param date 指定日期
     * @return
     */
    public static Long getMonthBegin(String date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date time = format.parse(date);
        Calendar c = Calendar.getInstance();
        c.setTime(time);

        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND, 0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return c.getTimeInMillis();
    }

    /**
     * 获取指定日期所在月份结束的时间戳
     *
     * @param date 指定日期
     * @return
     */
    public static Long getMonthEnd(String date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date time = format.parse(date);
        Calendar c = Calendar.getInstance();
        c.setTime(time);

        //设置为当月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        c.set(Calendar.MINUTE, 59);
        //将秒至59
        c.set(Calendar.SECOND, 59);
        //将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 获取本月最后一天的时间戳
        return c.getTimeInMillis();
    }


    /**
     * 上周每天的开始时间戳
     *
     * @param divisor
     * @return
     * @throws ParseException
     */
    public static Long getLastWeekDayBegin(int divisor) throws ParseException {
        Long weekBegin = getLastWeekBegin();
        return weekBegin + DayDIFF * (divisor - 1);
    }

    /**
     * 上周每天结束的时间戳
     *
     * @param divisor
     * @return
     * @throws ParseException
     */
    public static Long getLastWeekDayEnd(int divisor) throws ParseException {
        Long weekBegin = getLastWeekBegin();
        return weekBegin + DayDIFF * divisor - 1000;
    }

    /**
     * 本周每天开始的时间戳
     *
     * @param divisor
     * @return
     * @throws ParseException
     */
    public static Long getThisWeekDayBegin(int divisor) throws ParseException {
        Long weekBegin = getLastWeekBegin();
        return weekBegin + DayDIFF * (divisor + 6);
    }

    /**
     * 本周每天结束的时间戳
     *
     * @param divisor
     * @return
     * @throws ParseException
     */
    public static Long getThisWeekDayEnd(int divisor) throws ParseException {
        Long weekBegin = getLastWeekBegin();
        return weekBegin + DayDIFF * (divisor + 7) - 1000;
    }

    /**
     * 最近六个月每月开始的时间戳
     *
     * @param divisor
     * @return
     * @throws ParseException
     */
    public static Long getMonthBegin(int divisor) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);

        month = month - (divisor - 1);
        if (month <= 0) {
            month = 12 + month;
            year = year - 1;
        }


        return getMonthBegin(year + "-" + month + "-" + day);
    }

    /**
     * 最近六个月每月结束的时间戳
     *
     * @param divisor
     * @return
     * @throws ParseException
     */
    public static Long getMonthEnd(int divisor) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);

        month = month - (divisor - 1);
        if (month <= 0) {
            month = 12 + month;
            year = year - 1;
        }

        return getMonthEnd(year + "-" + month + "-" + day);
    }


    public static void main(String[] args) throws ParseException {
        System.out.println(DateUtil.getLastWeekDayBegin(1));
        System.out.println(DateUtil.getLastWeekDayEnd(1));

        System.out.println(DateUtil.getThisWeekDayBegin(1));
        System.out.println(DateUtil.getThisWeekDayEnd(1));

        System.out.println(DateUtil.getMonthBegin(6));
        System.out.println(DateUtil.getMonthEnd(6));

    }
}
